package com.community.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.community.dto.StorageGatherDto;
import com.community.dto.StoragePayDto;
import com.community.mapper.GatherDetailsMapper;
import com.community.mapper.GatherMapper;
import com.community.mapper.PayDetailsMapper;
import com.community.pojo.Gather;
import com.community.pojo.GatherDetails;
import com.community.pojo.Pay;
import com.community.mapper.PayMapper;
import com.community.pojo.PayDetails;
import com.community.service.IPayService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.community.utils.PageUtils;
import com.community.vo.StorageGatherTwoVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author TangHaoSong Fly ChenJie HeYuHang
 * @since 2024-05-15
 */
@Service
public class PayServiceImpl extends ServiceImpl<PayMapper, Pay> implements IPayService {

    @Autowired
    private PayMapper payMapper;

    @Autowired
    private PayDetailsMapper payDetailsMapper;

    @Override
    public PageUtils<Pay> twoShow(StorageGatherTwoVo vo, int currentPage, int pageSize) {
        QueryWrapper<Pay> qw=new QueryWrapper<>();
        qw.eq("CHECK_TAG","S001-3");
        //设置状态为已登记
        //qw.eq("STORE_TAG","K002-1");
        if(vo!=null){
            if(!StringUtils.isEmpty(vo.getReason())){
                qw.eq("REASON",vo.getReason());
            }
            if (!StringUtils.isEmpty(vo.getSearch_registerTime1())) {
                qw.ge("register_time", vo.getSearch_registerTime1());
            }
            if (!StringUtils.isEmpty(vo.getSearch_registerTime2())) {
                qw.le("register_time", vo.getSearch_registerTime2());
            }
        }
        PageHelper.startPage(currentPage, pageSize);
        List<Pay> list = payMapper.selectList(qw);
        PageInfo<Pay> p=new PageInfo<>(list);
        PageUtils<Pay> utils=new PageUtils<>();
        utils.setTotal(p.getTotal());
        utils.setRows(p.getList());
        return utils;
    }

    @Override
    public void page1(StoragePayDto dto) {
        this.save(dto);
        List<PayDetails> details = dto.getDetails();
        for(int i=0;i<details.size();i++){
            PayDetails payDetails = details.get(i);
            //每条id明细设置为null
            payDetails.setId(null);
            payDetails.setPayId(dto.getPayId());
//            storageGatherDetails.setGatherTag("K002-1");
            BigDecimal num=new BigDecimal(0);
            payDetails.setAmount(num);
            payDetailsMapper.insert(payDetails);
        }
    }

    @Override
    public String getMaxprocedure() {
        Pay pay = payMapper.queryMaxModule();
        if(pay==null){
            return null;
        }
        String designId = pay.getPayOrderNumber();
        return designId;
    }
}
